/**
 *
 * @author Roninwz
 * @date 2019/1/3 11:27
 * @since 1.0.0
 */

const myDateUtil = {};

const horDateFmtYMD = "yyyy-MM-dd";
const horDateFmtYMDHM = "yyyy-MM-dd HH:mm";
const horDateFmtYMDHMS = "yyyy-MM-dd HH:mm:ss";
const horDateFmtYMDHMSS = "yyyy-MM-dd HH:mm:ss.S";

const skewDateFmtYMD = "yyyy/MM/dd";
const skewDateFmtYMDHM = "yyyy/MM/dd HH:mm";
const skewDateFmtYMDHMS = "yyyy/MM/dd HH:mm:ss";
const skewDateFmtYMDHMSS = "yyyy/MM/dd HH:mm:ss.S";

/**
 * 将时间戳格式化
 * @param time  1546494479408
 * @returns {string}  2019-01-03 13:47:59
 */
myDateUtil.formatMyDate = function (time, fmt) {
  if (typeof(time) === "undefined") {
    return "";
  }

  //补0操作,当时间数据小于10的时候，给该数据前面加一个0
  function getzf(num) {
    if (parseInt(num) < 10) {
      num = '0' + num;
    }
    return num;
  }

  let oDate = new Date(time),
    oYear = oDate.getFullYear(),
    oMonth = oDate.getMonth() + 1,
    oDay = oDate.getDate(),
    oHour = oDate.getHours(),
    oMin = oDate.getMinutes(),
    oSen = oDate.getSeconds(),
    oSSen = oDate.getMilliseconds();
  let oTime;
  if (fmt === horDateFmtYMD) {
    oTime = oYear + '-' + getzf(oMonth) + '-' + getzf(oDay);
  } else if (fmt === horDateFmtYMDHM) {
    oTime = oYear + '-' + getzf(oMonth) + '-' + getzf(oDay) + ' ' + getzf(oHour) + ':' + getzf(oMin);
  } else if (fmt === horDateFmtYMDHMS) {
    oTime = oYear + '-' + getzf(oMonth) + '-' + getzf(oDay) + ' ' + getzf(oHour) + ':' + getzf(oMin) + ':' + getzf(oSen);
  } else if (fmt === horDateFmtYMDHMSS) {
    oTime = oYear + '-' + getzf(oMonth) + '-' + getzf(oDay) + ' ' + getzf(oHour) + ':' + getzf(oMin) + ':' + getzf(oSen) + '.' + oSSen;
  } else if (fmt === skewDateFmtYMD) {
    oTime = oYear + '/' + getzf(oMonth) + '/' + getzf(oDay);
  } else if (fmt === skewDateFmtYMDHM) {
    oTime = oYear + '/' + getzf(oMonth) + '/' + getzf(oDay) + ' ' + getzf(oHour) + ':' + getzf(oMin);
  } else if (fmt === skewDateFmtYMDHMS) {
    oTime = oYear + '/' + getzf(oMonth) + '/' + getzf(oDay) + ' ' + getzf(oHour) + ':' + getzf(oMin) + ':' + getzf(oSen);
  } else if (fmt === skewDateFmtYMDHMSS) {
    oTime = oYear + '/' + getzf(oMonth) + '/' + getzf(oDay) + ' ' + getzf(oHour) + ':' + getzf(oMin) + ':' + getzf(oSen) + '.' + oSSen;
  }
  return oTime;
};

/**
 *  获取当前时间 2019-01-03 13:30:03 570  到毫秒
 * @returns {string}
 */
myDateUtil.getNowFormatDateYMDHMSS = function () {
  let date = new Date();
  let seperator1 = "-";
  let seperator2 = ":";
  let month = date.getMonth() + 1;
  let strDate = date.getDate();

  function getzf(num) {
    if (parseInt(num) < 10) {
      num = '0' + num;
    }
    return num;
  }

  let currentDate = date.getFullYear() + seperator1 + getzf(month) + seperator1 + getzf(strDate)
    + " " + getzf(date.getHours()) + seperator2 + getzf(date.getMinutes())
    + seperator2 + getzf(date.getSeconds()) + '.' + date.getMilliseconds();
  return currentDate;
};

/**
 *  获取当前时间 2019-01-03 13:31:52  到秒
 * @returns {string}
 */
myDateUtil.getNowFormatDateYMDHMS = function () {
  let date = new Date();
  let seperator1 = "-";
  let seperator2 = ":";
  let month = date.getMonth() + 1;
  let strDate = date.getDate();

  function getzf(num) {
    if (parseInt(num) < 10) {
      num = '0' + num;
    }
    return num;
  }

  let currentdate = date.getFullYear() + seperator1 + getzf(month) + seperator1 + getzf(strDate)
    + " " + getzf(date.getHours()) + seperator2 + getzf(date.getMinutes())
    + seperator2 + getzf(date.getSeconds());
  return currentdate;
};

/**
 *  获取当前时间 2019-01-03 13:31  到分钟
 * @returns {string}
 */
myDateUtil.getNowFormatDateYMDHM = function () {
  let date = new Date();
  let seperator1 = "-";
  let seperator2 = ":";
  let month = date.getMonth() + 1;
  let strDate = date.getDate();

  function getzf(num) {
    if (parseInt(num) < 10) {
      num = '0' + num;
    }
    return num;
  }

  let currentdate = date.getFullYear() + seperator1 + getzf(month) + seperator1 + getzf(strDate)
    + " " + getzf(date.getHours()) + seperator2 + getzf(date.getMinutes());
  return currentdate;
};


/**
 *  获取当前时间 2019-01-03  到天
 * @returns {string}
 */
myDateUtil.getNowFormatDateYMD = function () {
  let date = new Date();
  let seperator1 = "-";
  let seperator2 = ":";
  let month = date.getMonth() + 1;
  let strDate = date.getDate();

  function getzf(num) {
    if (parseInt(num) < 10) {
      num = '0' + num;
    }
    return num;
  }

  let currentdate = date.getFullYear() + seperator1 + getzf(month) + seperator1 + getzf(strDate);
  return currentdate;
};

/**
 * 获取某年有多少天
 * @param dateStr
 * @returns {number}
 */
myDateUtil.getYearOfDay = function (dateStr) {
  let firstDayYear = myDateUtil.getFirstDayOfYear(dateStr);
  let lastDayYear = myDateUtil.getLastDayOfYear(dateStr);
  let fmtFirstDayYear = Date.parse(firstDayYear.replace(/-/g, '/'));
  let fmtLastDayYear = Date.parse(lastDayYear.replace(/-/g, '/'));
  let numSecond = (new Date(fmtLastDayYear).getTime() - new Date(fmtFirstDayYear).getTime()) / 1000;
  return Math.ceil(numSecond / (24 * 3600));
};


/**
 * 获取某年的第一天
 * @param dateStr  2018-01-02 或者 2018/01/02  或者 2018/1/2
 * @returns {string} 返回字符串
 */
myDateUtil.getFirstDayOfYear = function (dateStr) {
  let fmtDateStr = Date.parse(dateStr.replace(/-/g, '/'));
  let year = new Date(fmtDateStr).getFullYear();
  return year + "-01-01 00:00:00";
};


/**
 *  获取某年最后一天
 * @param dateStr 2018-01-02 或者 2018/01/02  或者 2018/1/2
 * @returns {string}  返回字符串
 */
myDateUtil.getLastDayOfYear = function (dateStr) {
  let fmtDateStr = Date.parse(dateStr.replace(/-/g, '/'));
  let year = new Date(fmtDateStr).getFullYear();
  let dateString = year + "-12-01 00:00:00";
  let endDay = myDateUtil.getMonthOfDay(dateString);
  return year + "-12-" + endDay + " 23:59:59";
};


/*获取某月有多少天*/
/**
 *
 * @param dateStr 2018-01-02 或者 2018/01/02  或者 2018/1/2
 * @returns {*}  返回整数
 */
myDateUtil.getMonthOfDay = function (dateStr) {
  let fmtDateStr = Date.parse(dateStr.replace(/-/g, '/'));
  let date = new Date(fmtDateStr);
  let year = date.getFullYear();
  let mouth = date.getMonth() + 1;
  let days;
  //当月份为二月时，根据闰年还是非闰年判断天数
  if (mouth === 2) {
    days = (year % 4 === 0 && year % 100 === 0 && year % 400 === 0) || (year % 4 === 0 && year % 100 !== 0) ? 28 : 29
  } else if (mouth === 1 || mouth === 3 || mouth === 5 || mouth === 7 || mouth === 8 || mouth === 10 || mouth === 12) {
    //月份为：1,3,5,7,8,10,12 时，为大月.则天数为31；
    days = 31
  } else {
    //其他月份，天数为：30.
    days = 30
  }
  return days
};


/*获取某个日期是当年中的第几天*/
myDateUtil.getDayOfYear = function (dateStr) {
  let fmtDateStr = Date.parse(dateStr.replace(/-/g, '/'));
  let firstDayYear = myDateUtil.getFirstDayOfYear(fmtDateStr);
  let fmtFirstDayYear = Date.parse(firstDayYear.replace(/-/g, '/'));
  let numSecond = (new Date(fmtDateStr).getTime() - new Date(fmtFirstDayYear).getTime()) / 1000;
  return Math.ceil(numSecond / (24 * 3600));
};

/**
 * 获取当前时间戳
 * @returns {number}
 */
myDateUtil.getNowTime = function () {
  let timestamp = new Date().getTime();
  return timestamp;
};

/**
 * 比较时间大小
 * 返回 -1 第二个大   返回 1 第一个大
 */
myDateUtil.compareTime = function (time1, time2) {
  let d1 = time1;
  let d2 = time2;
  if ((typeof d1) === "string") {
    d1 = new Date(Date.parse(d1.replace(/-/g, "/")));
  }
  if ((typeof d2) === "string") {
    d2 = new Date(Date.parse(d2.replace(/-/g, "/")));
  }
  let t1 = d1.getTime();
  let t2 = d2.getTime();
  if (t1 === t2) {
    return 0;
  } else if (t1 > t2) {
    return 1;
  }
  return -1;
};


/**
 * 是否闰年
 * @param year  整数
 * @returns {boolean}  返回true 闰年
 */
myDateUtil.isLeapYear = function (year) {
  return ((year % 4 === 0 && year % 100 !== 0) || year % 400 === 0);
};

/**
 * 是否是今天
 * @param date
 * @returns {boolean}
 */
myDateUtil.isToday = function (date) {
  let today = new Date();
  let todayTime = today.getTime() - today.getTime()%(1000*60*60*24);
  let dateTime = date.getTime();
  if(dateTime > todayTime && dateTime < (todayTime+(1000*60*60*24))){
    return true;
  }else{
    return false;
  }
};

/**
 * 把字符串日期转为日期
 * @param datetimeStr
 * @returns {Date}
 */
myDateUtil.convertStrToDate = function (datetimeStr) {
  let mydateint = Date.parse(datetimeStr);//数值格式的时间
  if (!isNaN(mydateint)) {
    let mydate = new Date(mydateint);
    return mydate;
  }
  let mydate = new Date(datetimeStr);//字符串格式时间
  let monthstr = mydate.getMonth() + 1;
  if (!isNaN(monthstr)) {//转化成功
    return mydate;
  }//字符串格式时间转化失败
  let dateParts = datetimeStr.split(" ");
  let dateToday = new Date();
  let year = dateToday.getFullYear();
  let month = dateToday.getMonth();
  let day = dateToday.getDate();
  if (dateParts.length >= 1) {
    let dataPart = dateParts[0].split("-");//yyyy-mm-dd  格式时间
    if (dataPart.length === 1) {
      dataPart = dateParts[0].split("/");//yyyy/mm/dd格式时间
    }
    if (dataPart.length === 3) {
      year = Math.floor(dataPart[0]);
      month = Math.floor(dataPart[1]) - 1;
      day = Math.floor(dataPart[2]);
    }
  }
  if (dateParts.length === 2) {//hh:mm:ss格式时间
    let timePart = dateParts[1].split(":");//hh:mm:ss格式时间
    if (timePart.length === 3) {
      let hour = Math.floor(timePart[0]);
      let minute = Math.floor(timePart[1]);
      let second = Math.floor(timePart[2]);
      return new Date(year, month, day, hour, minute, second);
    }
  }
  else {
    return new Date(year, month, day);
  }
};

module.exports = myDateUtil;
